package main

import (
    "errors"
    "fmt"
    "net"
    "net/rpc"
    "os"
)

type Args struct {
    A int
    B int
}

type Quotient struct {
    Quo int
    Rem int
}

type Arith int

func (t *Arith) Multiply2(args *Args, reply *int) error {
    *reply = args.A * args.B
    return nil
}

func (t *Arith) Divide2(args *Args, quo *Quotient) error {
    if args.B== 0 {
        return errors.New("divide by zero")
    } else {
        quo.Quo = args.A / args.B
        quo.Rem = args.A % args.B
        return nil
    }
}

func checkError(err error) {
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
        os.Exit(1)
    }
}

func main() {
    arith := new(Arith)
    rpc.Register(arith)

    tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
    checkError(err)

    listener, err := net.ListenTCP("tcp", tcpAddr)
    checkError(err)

    for {
        conn, err := listener.Accept()
        if err != nil {
            continue
        }
        rpc.ServeConn(conn)
    }
}
